<?php
/**
 * 数据格式验证类
 */
class ZDataValidator
{	
        /**
         * 验证方法
         * @param type $format
         * @param type $value
         * @param type $config
         * @return boolean
         */
        public static function validator($format, $value=null, $config=null){
            if(method_exists('ZDataValidator', $format))
                return call_user_func ("ZDataValidator::".$format, $value, $config);
            else
                return false;
        }
        /**
         * 邮箱
         * @param type $value
         * @param type $config
         * @return type
         */
        private static function email($value, $config){
            $config = is_string($config)?array('pattern'=>$config):$config;
            $pattern = '~^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$~';
            if(isset($config['pattern']))
                $pattern = $config['pattern'];
            return is_string($value) && strlen($value)<=254 && (preg_match($pattern,$value));
        }
        /**
         * 传真
         * @param type $value
         * @param type $config
         * @return type
         */
        private static function fax($value, $config){
            $config = is_string($config)?array('pattern'=>$config):$config;
            $pattern = '~^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$~';
            if(isset($config['pattern']))
                $pattern = $config['pattern'];
            return !empty($value)  && (preg_match($pattern,$value)); 
        }
        /**
         * 座机电话
         * @param type $value
         * @param type $config
         * @return type
         */
        private static function tel($value, $config){
            $config = is_string($config)?array('pattern'=>$config):$config;
            $pattern = '~^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$~';
            if(isset($config['pattern']))
                $pattern = $config['pattern'];
            return !empty($value)  && (preg_match($pattern,$value));
        }
        /**
         * 手机号
         * @param type $value
         * @param type $config
         * @return type
         */
        private static function mobile($value, $config){
            $config = is_string($config)?array('pattern'=>$config):$config;
            $pattern = '~^13[0-9]{9}|15[012356789][0-9]{8}|18[0256789][0-9]{8}|147[0-9]{8}$~';
            if(isset($config['pattern']))
                $pattern = $config['pattern'];
            return !empty($value)  && (preg_match($pattern,$value)); 
        }
        /**
         * 字符串
         * @param type $value
         * @param type $config
         * @return boolean
         */
        private static function string($value, $config){
            $allowEmpty = isset($config['allowEmpty'])?$config['allowEmpty']:false;
            $encoding = isset($config['encoding'])?$config['encoding']:false;
            if($allowEmpty && self::isEmpty($value))
                    return true;

            if(function_exists('mb_strlen') && isset($config['encoding']))
                    $length=mb_strlen($value, $encoding ? $encoding : 'utf-8');
            else
                    $length=strlen($value);
            if(isset($config['min']) && $length<$config['min'])
                    return false;
            if(isset($config['max']) && $length>$config['max'])
                    return false;
            if(isset($config['is']) && $length!==$config['is'])
                    return false;
            return true;
        }
        /**
         * 银行卡
         * @param type $value
         * @param type $config
         * @return type
         */
        private static function bankcard($value, $config){
            $config = is_string($config)?array('pattern'=>$config):$config;
            $pattern = '~^\d{16,19}$~';
            if(isset($config['pattern']))
                $pattern = $config['pattern'];
            return !empty($value)  && (preg_match($pattern,$value)); 
        }
        /**
         * 是否为空
         * @param type $value
         * @param type $trim
         * @return type
         */
        public static function isEmpty($value,$trim=false)
	{
            return $value===null || $value===array() || $value==='' || $trim && is_scalar($value) && trim($value)==='';
	}
        /**
         * 身份证号正则
         * @param type $value
         * @param type $config
         * @return type
         */
        public static function idcard($value,$config){
            $config = is_string($config)?array('pattern'=>$config):$config;
            $pattern = '~^[\d]{6}(19|20)*[\d]{2}((0[1-9])|(11|12))*[\d]{2}((0[1-9])|^2[\d]{1}([0-9])|(30|31))*[\d]{3}[xX]|[\d]{4}$~';
            if(isset($config['pattern']))
                $pattern = $config['pattern'];
            return !empty($value)  && (preg_match($pattern,$value));
        }
        /**
         * ajax返回格式
         * @param type $status
         * @param type $msg
         * @param type $ext
         */
        public static function ajaxResponse($status, $msg=null, $ext=null){
            echo json_encode(array('status'=>$status, 'msg'=>$msg,'ext'=>$ext));
            exit;
        }
}
